package com.xindaoapp.happypet.ui;

import java.util.List;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.Dayens;
import com.xindaoapp.happypet.bean.Dayens.Dayen;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.view.CircleImageView;
import com.xindaoapp.happypet.view.MyGridView;


/**
 * 达人榜(1.4 版本，第二个tab进入)
 * 
 * @author Yangqing
 * 
 */
public class DayenActivity extends BaseActivity {
    private ImageView vBackView;
    private TextView vRightTV, vAlldayen, vAllfans;
    //
    private MyGridView vDayen, vFans;


    private List<Dayen> dayen;
    private List<Dayen> fans;


    private boolean isFirst = true;
    private ImageView dayen_top_bg, fans_top_bg;


    @Override
    protected int getContentViewResId() {
        return R.layout.activity_dayen;
    }

    @Override
    protected void initViews() {
        dayen_top_bg = (ImageView) findViewById(R.id.dayen_top_bg);
        fans_top_bg = (ImageView) findViewById(R.id.fans_top_bg);
        dayen_top_bg.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                (int) ((float) CommonParameter.sScreenWidth / 612 * 70)));
        fans_top_bg.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                (int) ((float) CommonParameter.sScreenWidth / 612 * 70)));

        vBackView = (ImageView) findViewById(R.id.top_bar_left_imageview);
        vRightTV = (TextView) findViewById(R.id.top_bar_right_textview);
        vAlldayen = (TextView) findViewById(R.id.all_dayen);
        vAllfans = (TextView) findViewById(R.id.all_fans);
        vDayen = (MyGridView) findViewById(R.id.dayen);
        vFans = (MyGridView) findViewById(R.id.fans);
        // frameLayout = (FrameLayout) findViewById(R.id.frame_loading);

        vBackView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        vAlldayen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DayenActivity.this, DayenFansListActivity.class);
                intent.putExtra(Constants.DAYEN_FANS_TYPE, Constants.DAYEN_TYPE);
                startActivity(intent);
            }
        });
        vAllfans.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DayenActivity.this, DayenFansListActivity.class);
                intent.putExtra(Constants.DAYEN_FANS_TYPE, Constants.FANS_TYPE);
                startActivity(intent);
            }
        });
        setTopBarTitle("达人榜");
        // setTopBarImage(R.drawable.bang_top_image);
        super.initViews();
    }

    @Override
    protected void initEvents() {
        super.initEvents();
    }

    @Override
    protected void onLoadDatas() {
        getDaYenData();
        getFansData();
        vDayen.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != dayen && dayen.size() > 0) {
                    Intent intent = new Intent(DayenActivity.this, MyUserCenterActivity.class);
                    intent.putExtra(Constants.KEY_UID, dayen.get(position).uid);
                    intent.putExtra(Constants.KEY_USERNAME, dayen.get(position).username);
                    intent.putExtra(Constants.KEY_USERFACE, dayen.get(position).userface);
                    startActivity(intent);
                }
            }
        });
        vFans.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != fans && fans.size() > 0) {
                    Intent intent = new Intent(DayenActivity.this, MyUserCenterActivity.class);
                    intent.putExtra(Constants.KEY_UID, fans.get(position).uid);
                    intent.putExtra(Constants.KEY_USERNAME, fans.get(position).username);
                    intent.putExtra(Constants.KEY_USERFACE, fans.get(position).userface);
                    startActivity(intent);
                }
            }
        });
        super.onLoadDatas();
    }



    private void getFansData() {
        if (CommonUtil.checkNetState(DayenActivity.this)) {
            HappyPetApplication.get().getMoccaApi()
                    .getDaYenDatas("1", "8", "2", new IRequest<Dayens>() {
                        @Override
                        public void request(Dayens t) {
                            if (null == t && isFirst) {
                                onDataArrived(false);
                            } else {
                                isFirst = false;
                                if (t.getResult().equals("0")) {
                                    vAllfans.setText(String.format("全部%s人", t.total));

                                    onDataArrived(true);
                                    if (null != t.array && t.array.size() > 0) {
                                        fans = t.array;
                                        vFans.setAdapter(new DaYenAdapter(fans));
                                    }
                                } else {
                                    if (isFirst) {
                                        onDataArrived(false);
                                    }
                                    if (!TextUtils.isEmpty(t.getMsg())) {
                                        Toast.makeText(DayenActivity.this, t.getMsg(),
                                                Toast.LENGTH_SHORT).show();

                                    }
                                }
                            }
                        }
                    });
        } else {
            onDataArrived(false);
        }
    }

    private void getDaYenData() {
        if (CommonUtil.checkNetState(getApplicationContext())) {
            HappyPetApplication.get().getMoccaApi()
                    .getDaYenDatas("1", "8", "1", new IRequest<Dayens>() {
                        @Override
                        public void request(Dayens t) {
                            if (null == t) {

                            } else {
                                if (t.getResult().equals("0")) {
                                    if (null != t.array && t.array.size() > 0) {
                                        vAlldayen.setText(String.format("全部%s人", t.total));
                                        dayen = t.array;
                                        vDayen.setAdapter(new DaYenAdapter(dayen));
                                    }
                                } else {
                                    if (!TextUtils.isEmpty(t.getMsg())) {
                                        Toast.makeText(DayenActivity.this, t.getMsg(),
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }

                            }
                        }
                    });
        }
    }

    private class DaYenAdapter extends BaseAdapter {
        private final List<Dayen> datas;

        public DaYenAdapter(List<Dayen> datas) {
            super();
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(DayenActivity.this, R.layout.left_nemu_list, null);
                holder.iv_item_list = (CircleImageView) convertView.findViewById(R.id.iv_item_list);
                holder.tv_item_list = (TextView) convertView.findViewById(R.id.tv_item_list);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ImageLoader.getInstance().displayImage(datas.get(position).userface,
                    holder.iv_item_list);
            holder.tv_item_list.setText(datas.get(position).username);
            holder.tv_item_list.setTextColor(android.graphics.Color.BLACK);
            return convertView;
        }
    }
    private class ViewHolder {
        CircleImageView iv_item_list;
        TextView tv_item_list;
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }

    @Override
    protected int getTopBarRightTextViewRes() {
        return R.string.darenguize;
    }

    // @Override
    // protected int getTopBarRightTextViewRes() {
    // // TODO Auto-generated method stub
    // return R.string.darenguize;
    // }

    @Override
    protected OnClickListener getRightViewClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DayenActivity.this, GuiZeActivity.class);
                intent.putExtra(Constants.GUIZE, "daren");
                startActivity(intent);
            }
        };
    }
}
